package client;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.Hashtable;
import com.google.gson.Gson;
import javafx.scene.control.TreeCell;
import javafx.scene.control.TreeItem;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import model.*;

public class TreeCellRenderer extends TreeCell<String> {
	 private Message mes;

	    public TreeCellRenderer(Message mes){
	    	this.mes=mes;
	    }
	    //updateItem方法会遍历该节点的所有子节点，递归调用updateItem方法来渲染每个子节点
	    //更新树节点内容的函数（头像+文本内容）
	    protected void updateItem(String item, boolean empty) {
	        super.updateItem(item, empty);
	        if(empty || item == null) {
	            setGraphic(null);
	            setText(null);
	            return;
	        }
	        TreeItem<String> node = (TreeItem<String>) getTreeItem();
	        String str = node.getValue();   
	        Gson gson = new Gson();
	        
	        String[] array = null;
            String friendaccount = "";
	        String friendname = "";
            
	        if(!str.equals("ChatGPT") && !str.equals("聊天") && !str.equals("我的好友") && !str.equals("我的群聊") ) {
	        	 array = str.split("\\(");
	             friendaccount = array[1].substring(0, array[1].length()-1);
	             friendname = array[0];
	        }    
	      	      	        
	      //定义两个Type，使得能够正确将json反序列化
        	Type hashtableType1 = new ParameterizedType() {
                public Type[] getActualTypeArguments() {
                    return new Type[]{Integer.class, String.class};
                }

                public Type getRawType() {
                    return Hashtable.class;
                }

                public Type getOwnerType() {
                    return null;
                }
            };
            
            Type hashtableType2 = new ParameterizedType() {
                public Type[] getActualTypeArguments() {
                    return new Type[]{Integer.class, Boolean.class};
                }

                public Type getRawType() {
                    return Hashtable.class;
                }

                public Type getOwnerType() {
                    return null;
                }
            };
                       
	        
	        if(mes.getType() == MsgType.LOGIN_SUCCEED) {
	        	
	        	Boolean online_flag = false;
	        	
	            if (node.isLeaf()) {   	
	            	           	            	
	            if(!str.equals("我的好友") && !str.equals("我的群聊")) {
	            	
	            String fjson = mes.getText();
	            String sjson = mes.getStates();
	            String rjson = mes.getRooms();
	            Hashtable<Integer, String> friends = gson.fromJson(fjson, hashtableType1);   //此用户的所有好友，无论是否在线
	            Hashtable<Integer, Boolean> states = gson.fromJson(sjson, hashtableType2);   //̬此用户所有好友的在线状态
	            Hashtable<Integer, String> rooms = gson.fromJson(rjson, hashtableType1);
	            
	            if(str.equals("ChatGPT")) {
	            	ImageView onlineImgView = new ImageView(new Image("file:image/dialogimage/chatGPT.jpg"));
	            	onlineImgView.setFitHeight(30);
                    onlineImgView.setFitWidth(30);
                    setGraphic(onlineImgView);
                    online_flag = true;
	            }
	            
	            for(Integer i : friends.keySet()) {
	                String fAaccount = String.valueOf(i);
	            	
                	if(friendaccount.equals(fAaccount) && states.get(i).equals(true)) {
                		 ImageView onlineImgView = new ImageView(new Image("file:image/dialogimage/touxiangl.png"));
                            onlineImgView.setFitHeight(30);
                            onlineImgView.setFitWidth(30);
                            setGraphic(onlineImgView);
                            online_flag = true;
                	}
                }
	            
	            for(Integer i : rooms.keySet()) {
	            	String roomAccount = String.valueOf(i);

                	if(friendaccount.equals(roomAccount)) {
                		 ImageView onlineImgView = new ImageView(new Image("file:image/dialogimage/qunliao.png"));
                            onlineImgView.setFitHeight(30);
                            onlineImgView.setFitWidth(30);
                            setGraphic(onlineImgView);
                            online_flag = true;
                	}
                }
	            
	            if(!online_flag) {
               	 ImageView imgView = new ImageView(new Image("file:image/dialogimage/touxianga.png"));
                    imgView.setFitHeight(30);
                    imgView.setFitWidth(30);
                    setGraphic(imgView);
	            	}
	            }
	          }  
	        //已获取到在线好友列表
	        } else if(mes.getType() == MsgType.UPDATE_FRIENDLIST) {
	        	
	        	Boolean online_flag = false;	        	        	
	            String fjson = mes.getText();
	            String sjson = mes.getStates();
	            String rjson = mes.getRooms();
	            
	            Hashtable<Integer, String> friends = gson.fromJson(fjson, hashtableType1);
	            Hashtable<Integer, Boolean> states = gson.fromJson(sjson, hashtableType2);
	            Hashtable<Integer, String> rooms = gson.fromJson(rjson, hashtableType1);
	            	            
	            if (node.isLeaf()) {
	            	if(!str.equals("我的好友") && !str.equals("我的群聊")) {	                    	                    
	            		if(str.equals("ChatGPT")) {
	    	            	ImageView onlineImgView = new ImageView(new Image("file:image/dialogimage/chatGPT.jpg"));
	    	            	onlineImgView.setFitHeight(30);
	                        onlineImgView.setFitWidth(30);
	                        setGraphic(onlineImgView);
	                        online_flag = true;
	    	            }
	            		
	            		
	                    //将在线好友的头像设置为亮
	                    for(Integer i : friends.keySet()) {
	                   	                    	
	                    	String fname = friends.get(i);
	    	                String fAaccount = String.valueOf(i);
	    	            	
	                    	if(friendaccount.equals(fAaccount) && states.get(i).equals(true)) {
	                    		 ImageView onlineImgView = new ImageView(new Image("file:image/dialogimage/touxiangl.png"));
	                                onlineImgView.setFitHeight(30);
	                                onlineImgView.setFitWidth(30);
	                                setGraphic(onlineImgView);
	                                online_flag = true;
	                                
	                                if(!friendname.equals(fname)) {
	                                	friendname = fname;
	                                }   
	                    	}
	                    }
	                    
	                    for(Integer i : rooms.keySet()) {
	    	            	String roomName = rooms.get(i);
	    	            	String roomAccount = String.valueOf(i);       

	                    	if(friendaccount.equals(roomAccount)) {
	                    		 ImageView onlineImgView = new ImageView(new Image("file:image/dialogimage/qunliao.png"));
	                                onlineImgView.setFitHeight(30);
	                                onlineImgView.setFitWidth(30);
	                                setGraphic(onlineImgView);
	                                online_flag = true;
	                                
	                                if(!friendname.equals(roomName)) {
	                                	friendname = roomName;
	                                }   
	                    	}
	                    }
	                    
	                    if(!online_flag) {
	                    	 ImageView imgView = new ImageView(new Image("file:image/dialogimage/touxianga.png"));
	 	                    imgView.setFitHeight(30);
	 	                    imgView.setFitWidth(30);
	 	                    setGraphic(imgView);
	                    }
	                   	                  	                    
	            	}           	
	            } 
	        }else if(mes.getType() == MsgType.REG_SUCCEED) {
	        	if(!str.equals("我的好友") && !str.equals("我的群聊")) {                    	                    
            		if(str.equals("ChatGPT")) {
    	            	ImageView onlineImgView = new ImageView(new Image("file:image/dialogimage/chatGPT.jpg"));
    	            	onlineImgView.setFitHeight(30);
                        onlineImgView.setFitWidth(30);
                        setGraphic(onlineImgView);                      
    	            }
	        	}
	        }
	        if(!str.equals("ChatGPT") && !str.equals("聊天") && !str.equals("我的好友") && !str.equals("我的群聊") ) {
	        	setText(friendname+"("+friendaccount+")");
	        }else {
	        	setText(str);
	        }	        
	    }
}
